package com.tadpole.music.view.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.me.MemberCenterAdapter;
import com.tadpole.music.bean.me.VipListBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.VipListIView;
import com.tadpole.music.presenter.me.VipListPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.SimulationPracticeActivity;

import java.util.List;

/**
 * 会员中心
 * Created By 孙岩
 * on 2020/9/11
 */
public class MemberCenterActivity extends BaseActivity  implements VipListIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private MemberCenterAdapter memberCenterAdapter;
    //presenter
    private VipListPresenter vipListPresenter;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //是从我的跳转还是其他跳转
    private String flag = "";//1是其他（带参数college_id，rank_id） 2是我的（不带参数）
    //学院id
    private String college_id;
    //等级id
    private String rank_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_center);

        flag = getIntent().getStringExtra("flag");

        if ("1".equals(flag)) {//带参数
            college_id = getIntent().getStringExtra("college_id");
            rank_id = getIntent().getStringExtra("rank_id");
        }

        initViews();
        initListeners();
        vipListPresenter = new VipListPresenter();
        vipListPresenter.attachView(this);
        vipListPresenter.getVipList(flag,college_id,rank_id,false);
    }


    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("会员中心");
        recyclerView = findViewById(R.id.recyclerView);
        rl_refresh = findViewById(R.id.rl_refresh);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                MemberCenterActivity.this.finish();
            }
        });

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                vipListPresenter.getVipList(flag,college_id,rank_id,true);
            }
        });
    }

    @Override
    public void show401() {
        Intent intent = new Intent(MemberCenterActivity.this,LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN,"");
    }

    @Override
    public void showVipList(final List<VipListBean.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        memberCenterAdapter = new MemberCenterAdapter(this,list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(memberCenterAdapter);

        memberCenterAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(MemberCenterActivity.this).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(MemberCenterActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }else{
                        if(list.get(position).isPaid()){//已经买过
                            Intent intent = new Intent(MemberCenterActivity.this, SimulationPracticeActivity.class);
                            intent.putExtra("id", list.get(position).getCollege_id());
                            intent.putExtra("name", list.get(position).getCollege().getName());
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(MemberCenterActivity.this,PayDetailActivity.class);
                            intent.putExtra("goods_id",String.valueOf(list.get(position).getId()));
                            intent.putExtra("college_id",String.valueOf(list.get(position).getCollege_id()));
                            intent.putExtra("name",list.get(position).getName());
                            intent.putExtra("uname",list.get(position).getCollege().getName());
                            intent.putExtra("desc",list.get(position).getDesc());
                            intent.putExtra("price",String.format("%.2f", Double.valueOf(list.get(position).getPrice())));
                            intent.putExtra("flag","1");
                            startActivity(intent);

                        }
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        memberCenterAdapter.setOnChildClickListener(new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(View v, int position) {
                if(v.getId()==R.id.tvGo){
                    if(Mutils.isNetworkAvailable()){
                        if(SpUtil.getInstance(MemberCenterActivity.this).getString(Constant.TOKEN,"").equals("")){
                            Intent intent = new Intent(MemberCenterActivity.this,LoginActivity.class);
                            startActivity(intent);
                        }else{
                            if(list.get(position).isPaid()){//已经买过
                                Intent intent = new Intent(MemberCenterActivity.this, SimulationPracticeActivity.class);
                                intent.putExtra("id", list.get(position).getCollege_id());
                                intent.putExtra("name", list.get(position).getCollege().getName());
                                startActivity(intent);
                            }else{
                                Intent intent = new Intent(MemberCenterActivity.this,PayDetailActivity.class);
                                intent.putExtra("goods_id",String.valueOf(list.get(position).getId()));
                                intent.putExtra("college_id",String.valueOf(list.get(position).getCollege_id()));
                                intent.putExtra("name",list.get(position).getName());
                                intent.putExtra("uname",list.get(position).getCollege().getName());
                                intent.putExtra("desc",list.get(position).getDesc());
                                intent.putExtra("price",String.format("%.2f", Double.valueOf(list.get(position).getPrice())));
                                intent.putExtra("flag","1");
                                startActivity(intent);
                            }
                        }
                    }else{
                        ToastUtils.show("请检查您的网络设置");
                    }
                }
            }
        });
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        vipListPresenter.getVipList(flag,college_id,rank_id,false);
    }
}
